package membe

import (
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/logic"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/pkg/cache"
	"github.com/vueadmin/utils"
	"github.com/vueadmin/utils/common/request"
	"github.com/vueadmin/utils/common/result"
	"github.com/vueadmin/utils/conv"
	"github.com/vueadmin/utils/response"
	"math"
)

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func GetPageList(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembePageReq{
		PageReq:request.PageReq{Page: 1,Limit: 20},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	list, count, err := l.GetPageList(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", result.M{
		"data":  list,
		"total": count,
		"sum_amount": l.GetAmountSum(&req),
	})
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func UpdateExt(c *gin.Context, svcCtx *svc.ServiceContext) {
	var req map[string]interface{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.UpdateExt(req);err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func Add(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeAddReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	ret, err := l.Add(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", ret)
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func Update(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeUpdateReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Update(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func GetUpdateInfo(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeGetUpdateInfoReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
		}
	l := logic.NewMembeLogic(c,svcCtx)
	info,err := l.GetUpdateInfo(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", info)
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func Delete(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeDeleteReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Delete(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "删除成功", nil)
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func Detail(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeDetailReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	info,err := l.Detail(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", info)
}

/**
 * @description(重置密码)
 * @buildcode(true)
*/
func ResetPwd(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeResetPwdReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.ResetPwd(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(导入)
 * @buildcode(true)
*/
func Import(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := []dto.MembeImportReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Import(req);err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "导入成功", nil)
}

/**
 * @description(客户端导出)
 * @buildcode(true)
*/
func Export(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembePageReq{
		PageReq: request.PageReq{Page: 1, Limit: 200},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	list,count,err := l.Export(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	totalPage := math.Ceil(conv.Float64(count)/conv.Float64(req.Limit))

	data := make(map[string]interface{})
	data["filename"] = "会员管理.xlsx"
	data["percentage"] = math.Ceil(conv.Float64(req.Page*100) / conv.Float64(totalPage))
	data["header"] = conv.StringToSlice("用户名,性别,手机号,头像,邮箱,密码,积分,状态,省市区,创建时间",",")
	data["data"] = list
	data["status"] = 200

	if req.Page >= conv.Int(totalPage){
		cache.New(svcCtx.Redis).Delete("membe_count")
	}
	response.Success(c, "success", data)
}

/**
 * @description(充值)
 * @buildcode(true)
*/
func Jia(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeJiaReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Jia(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "操作成功", nil)
}

/**
 * @description(扣除)
 * @buildcode(true)
*/
func Jian(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeJianReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Jian(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "操作成功", nil)
}

/**
 * @description(状态禁用)
 * @buildcode(true)
*/
func Forbidden(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeForbiddenReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Forbidden(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

